package com.mall.user.services;

import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.MemberConverter;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberConverter memberConverter;



    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        UserLoginResponse userLoginResponse = new UserLoginResponse();

        userLoginRequest.requestCheck();
        Example example = new Example(Member.class);
        String ups = DigestUtils.md5DigestAsHex(userLoginRequest.getPassword().getBytes());
        example.createCriteria().andEqualTo("username",userLoginRequest.getUserName()).andEqualTo("password",ups);
        List<Member> members = memberMapper.selectByExample(example);
        if(members.size()!=1)
        {
            //用户名密码错误
            userLoginResponse.setCode(SysRetCodeConstants.USERORPASSWORD_ERRROR.getCode());
            userLoginResponse.setMsg(SysRetCodeConstants.USERORPASSWORD_ERRROR.getMessage());
            return userLoginResponse;
        }
        log.info("登录成功{}",userLoginRequest);
        userLoginResponse = memberConverter.member2Login(members.get(0));
        userLoginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        userLoginResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        //3.生成token
        String token = JwtTokenUtils.builder().msg(userLoginRequest.getUserName()+","+userLoginResponse.getId()).build().creatJwtToken();
        log.info("生成token：{}",token);
        userLoginResponse.setToken(token);
//        RBucket rBucket = redissonClient.getBucket("access_token"+userLoginResponse.getUsername());
//        rBucket.set(token);
        return userLoginResponse;
    }

    @Override
    public String getTokenInfo(String token) {

        String info = JwtTokenUtils.builder().token(token).build().freeJwt();
        return info;
    }

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        String token = checkAuthRequest.getToken();
        String msg = JwtTokenUtils.builder().token(token).build().freeJwt();
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        checkAuthResponse.setUserinfo(msg);
        checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        checkAuthResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return checkAuthResponse;
    }
}
